package com.sk.sourcecircle.activity.mine.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.PersonDetailsActivity;
import com.sk.sourcecircle.adapter.NewFriendAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.MyFriendBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/11/23.
 */

public class NewFriendActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private int page = 1;
    private NewFriendAdapter adapter;
    private List<MyFriendBean.DataBeanX.DataBean> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;

    private LoadingDialog loadingDialog;
    private int pos;

    @Override
    protected int getContentView() {
        return R.layout.activity_new_friend;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new NewFriendAdapter(R.layout.item_new_friend, allDataList);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
        loadingDialog.show();
        AppApi.myFriend(page, 0, 2,this, 0, this);

    }

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                MyFriendBean myFriendBean = JSON.parseObject(result, MyFriendBean.class);
                List<MyFriendBean.DataBeanX.DataBean> dataBeanList = myFriendBean.getData().getData();
                allDataList.addAll(dataBeanList);
                if (allDataList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();
                break;
            case 2:
                allDataList.get(pos).setStatus(1);
                adapter.notifyItemChanged(pos);
                break;
        }


    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, PersonDetailsActivity.class);
        intent.putExtra("id", allDataList.get(position).getUser_id());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.myFriend(page, 0, 2,this, 0, this);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.pass:
                pos = position;
                loadingDialog.show();
                AppApi.doFriend(allDataList.get(position).getUser_id(), 1, this, 2, NewFriendActivity.this);
                break;
        }
    }


}
